<?php
/**
 * Enter description here ...
 * @author Micou13630
 *
 */
class ds_hierarchy implements IDataAccessDao {
	/**
	 * Constructeur
	 */
	public function ds_hierarchy() {
		
		if (!isset($_SESSION['dataset'])) {
			$_SESSION['dataset'] = array();
			$_SESSION['dataset'][] = array('ref'=>0,'name'=>'Enterprise','refParent'=>null,'isFolder'=>true);
			$_SESSION['dataset'][] = array('ref'=>1,'name'=>'Directors','refParent'=>0,'isFolder'=>true);
			$_SESSION['dataset'][] = array('ref'=>2,'name'=>'Mechanics','refParent'=>0,'isFolder'=>true);
			$_SESSION['dataset'][] = array('ref'=>3,'name'=>'Engineers','refParent'=>0,'isFolder'=>true);
			$_SESSION['dataset'][] = array('ref'=>10,'name'=>'Cpt. Kirk','refParent'=>1,'isFolder'=>false);
			$_SESSION['dataset'][] = array('ref'=>20,'name'=>'Scott','refParent'=>2,'isFolder'=>false);
			$_SESSION['dataset'][] = array('ref'=>21,'name'=>'Sulu','refParent'=>2,'isFolder'=>false);
			$_SESSION['dataset'][] = array('ref'=>30,'name'=>'Spock','refParent'=>3,'isFolder'=>false);
			$_SESSION['dataset'][] = array('ref'=>31,'name'=>'Uhura','refParent'=>3,'isFolder'=>false);
		}
	}
	
	/**
	 * Lecture de donnee
	 * @param IData $data
	 */
	public function fetch(IRequestDao $requestDao) {
		$fv = $requestDao->getFieldsValue();
		if (isset($fv['refParent'])) {
			
			$t = array();
			foreach ($_SESSION['dataset'] as $cas) {
				if ($cas['refParent'] == $fv['refParent']) {
					$t[] = $cas;
				}
			}
			return new DataSetDao($t);
		}
		// Constitution du dataset
		return new DataSetDao($_SESSION['dataset']);
	}
	
	/**
	 * Ajouter un item
	 * @param IData $data
	 */
	public function add(IRequestDao $requestDao) {
		$data = $requestDao->getFieldsValue();
		$data['isFolder'] = isset($data['isFolder']);
		
		// Clef vide
		if (!isset($data['ref'])) {
			$err = new ErrorValidationDao();
			$err->addMessage('ref','La clé doit avoir une valeur');
			return $err;
		}
		
		$tmp = date();
		foreach ($_SESSION['dataset'] as $cas) {
			error_log("ADD[".$tmp."]...".$cas['ref']);
		}
		
		// Clef dupliquée
		foreach ($_SESSION['dataset'] as $cas) {
			if ($cas['ref'] == $data['ref']) {
				$err = new ErrorValidationDao();
				$err->addMessage('ref','Clé primaire dupliquée !');
				return $err;
			}
		}
		
		// Clef parente existante
		$b = false;
		foreach ($_SESSION['dataset'] as $cas) {
			//error_log("TEST...".$cas['ref']." | ".$data['refParent']);
			if ($cas['ref'] == $data['refParent']) {
				$b = true;
				break;
			}
		}
		if (!$b) {
			$err = new ErrorValidationDao();
			$err->addMessage('refParent','Clef ('.$data['refParent'].') parente inexistante !');
			return $err;
		}

		$_SESSION['dataset'][] = $data;
		return new DataSetDao($data);
	}
	
	/**
	 * Effacer un item
	 * @param IData $data
	 */
	public function remove(IRequestDao $requestDao) {
		$data = $requestDao->getFieldsValue();
		$tabOut = array();
		foreach ($_SESSION['dataset'] as $cas) {
			if ($cas['ref'] != $data['ref'] && $cas['refParent'] != $data['ref']) {
				$tabOut[] = $cas;
			}
		}
		
		$_SESSION['dataset'] = $tabOut;
		
		return new DataSetDao($data);
	}
	
	/**
	 * Modifier un item
	 * @param IData $data
	 */
	public function update(IRequestDao $requestDao) {
		$data = $requestDao->getFieldsValue();
		$tabOut = array();
		foreach ($_SESSION['dataset'] as $cas) {
			if ($cas['ref'] === $data['ref']) {
				$tabOut[] = $data;
			} else {
				$tabOut[] = $cas;
			}
		}
		
		$_SESSION['dataset'] = $tabOut;
		
		return new DataSetDao($data);
	}
	
	/**
	 * Renvoie le ou les champs constituant la clef primaire
	 * Enter description here ...
	 */
	public function primaryKeyFields() {
		return 'ref';
	}
}
?>
